package gupao.concurrency.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class SimpleDeadLockSyncSample {
    static Object workLock = new Object();
    static Object sleepLock = new Object();

    static List<Object> listLock = new ArrayList<>();

    public static void workAndSleep() {
        synchronized (listLock){
            if(listLock.contains(workLock) || listLock.contains(sleepLock)){
                //
            }else {
                listLock.add(workLock);
                listLock.add(sleepLock);
                try{

                }finally {
                    listLock.remove(workLock);
                    listLock.remove(sleepLock);
                }
            }
        }
        /*synchronized (workLock) {
            log.info("do some work");
            synchronized (sleepLock) {
                log.info("have some sleep");
            }
        }*/
    }

    public static void sleepAndWork() {
        synchronized (sleepLock) {
            log.info("have some sleep");
            synchronized (workLock) {
                log.info("do some work");
            }
        }
    }

}
